package com.ruoyi.web.controller.emp;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.EmpConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EmpArea;
import com.ruoyi.system.service.IEmpAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 行政区域Controller
 *
 * @author zsl
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/system/area")
@Slf4j
public class EmpAreaController extends BaseController {
    @Autowired
    private IEmpAreaService empAreaService;

    @Autowired
    private EmpConfig empConfig;

    /**
     * 查询行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:list')")
    @GetMapping("/list")
    public AjaxResult list(EmpArea empArea) {
        //开启分页
        int empAreaMaxSize = empConfig.getEmpAreaMaxSize();
        log.info("查询行政区域列表-查询数量:{}", empAreaMaxSize);
        log.info("查询行政区域列表-查询条件:{}", JSON.toJSONString(empArea));
        Page<EmpArea> page = new Page<>(1, empAreaMaxSize);
        LambdaQueryWrapper<EmpArea> wrapper = Wrappers.lambdaQuery(EmpArea.class)
                .eq(StringUtils.isNotBlank(empArea.getCode()), EmpArea::getCode, empArea.getCode())
                .like(StringUtils.isNotBlank(empArea.getName()), EmpArea::getName, empArea.getName())
                .orderByAsc(EmpArea::getCode);
        List<EmpArea> records = empAreaService.page(page, wrapper).getRecords();
        return success(records);
    }

    /**
     * 导出行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:export')")
    @Log(title = "行政区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpArea empArea) {
        List<EmpArea> list = empAreaService.selectEmpAreaList(empArea);
        ExcelUtil<EmpArea> util = new ExcelUtil<EmpArea>(EmpArea.class);
        util.exportExcel(response, list, "行政区域数据");
    }

    /**
     * 获取行政区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(empAreaService.selectEmpAreaById(id));
    }

    /**
     * 新增行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:add')")
    @Log(title = "行政区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpArea empArea) {
        return toAjax(empAreaService.insertEmpArea(empArea));
    }

    /**
     * 修改行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:edit')")
    @Log(title = "行政区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpArea empArea) {
        return toAjax(empAreaService.updateEmpArea(empArea));
    }

    /**
     * 删除行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:remove')")
    @Log(title = "行政区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(empAreaService.deleteEmpAreaByIds(ids));
    }
}
